package com.school.gift.controller.admin;

import com.github.pagehelper.PageInfo;
import com.school.gift.domain.Evaluate;
import com.school.gift.domain.Order;
import com.school.gift.model.OrderModel;
import com.school.gift.service.EvaluateService;
import com.school.gift.service.OrderService;
import com.school.support.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.common.support.base.Page;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;
import zhibi.common.util.DateUtil;
import zhibi.common.util.StringUtil;
import zhibi.common.util.poi.ExcelUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * Created by 执笔 on 2017/3/17.
 */
@Controller
@RequestMapping("admin/order")
public class MOrderController extends AdminBaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private EvaluateService evaluateService;

    @RequestMapping("list")
    public String list(Model model, Page page, OrderModel orderModel) {
        Example example = ExampleUtil.getInstance()
                .addParam("o.orderNo", orderModel.getOrderno())
                .addOrder("o.status")
                .getExample();
        PageInfo<OrderModel> pageInfo = orderService.selectModel(example, page);
        setModelAttribute(model, pageInfo);
        return "admin/order/list";
    }

    @RequestMapping("sendGoods/{id}")
    public String sendGoods(@PathVariable Long id) {
        Order order = orderService.selectByPK(id);
        order.setStatus(3);
        orderService.updateByIdSelective(order);
        return redirect("list");
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        OrderModel orderModel = orderService.selectModelById(id);
        Evaluate evaluate =evaluateService.selectByOrderId(id);
        model.addAttribute("evaluate",evaluate);
        model.addAttribute(orderModel);
        return "admin/order/detail";
    }

    @RequestMapping("monthList")
    public String monthList(String month, Page page, Model model) {
        if (StringUtil.isEmpty(month)) {
            month = DateUtil.dateToString(new Date(), "yyyy-MM");
        }
        Example example = ExampleUtil.getInstance()
                .addParam("o.addtime", month, ExampleType.Operation.LIKE)
                .addParam("o.status", STATE_2, ExampleType.Operation.GTE)
                .addOrder("o.addtime")
                .getExample();
        PageInfo<OrderModel> pageInfo = orderService.selectModel(example, page);
        Double money = orderService.selectSumByDate(month);
        setModelAttribute(model, pageInfo);
        model.addAttribute("money", money == null ? 0d : money);
        return "admin/order/month-list";
    }

    @RequestMapping(value = "monthList",params = "export")
    public void export(String month, HttpServletResponse response) throws Exception {
        if (StringUtil.isEmpty(month)) {
            month = DateUtil.dateToString(new Date(), "yyyy-MM");
        }
        Example example = ExampleUtil.getInstance()
                .addParam("o.addtime", month, ExampleType.Operation.LIKE)
                .addParam("o.status", STATE_2, ExampleType.Operation.GTE)
                .addOrder("o.addtime")
                .getExample();
        PageInfo<OrderModel> pageInfo = orderService.selectModel(example, new Page(Integer.MAX_VALUE));
        exportExcel(response,new String[]{"订单号","礼品名","用户名","数量","总价","发起时间","支付时间","发货时间"},pageInfo.getList(),new String[]{"orderno","goodsName","username","count","total","addtime","paytime","sendtime"},OrderModel.class);
    }
}
